package cs348a.opengl.object;

import java.awt.Color;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

import cs348a.data.Site;

/**
 * OpenGLObject for a Site
 * 
 * @author H.S Kim
 */
public class OpenGLSite extends AbstractOpenGLObject{

	/** The site */
	private Site site;
	
	/** The quad */
	private GLUquadric theQuad;
	
	private Color onColor = Color.white;
	private Color offColor = Color.yellow;
	private Color siteColor = Color.red;
	
	private double radius = 10;
    private int slices = 20;
    private int stacks = 20;
	
    /**
     * Constructor
     * 
     * @param s
     * @param q
     */
	public OpenGLSite(Site s, GLUquadric q) {
		site = s;
		theQuad = q;
	}
	
	/**
	 * Sets the on-point color
	 * 
	 * @param c
	 */
	public void onColor(Color c) {
		onColor = c;
	}
	
	/**
	 * Sets the off-point color
	 * 
	 * @param c
	 */
	public void offColor(Color c) {
		offColor = c;
	}
	
	/**
	 * Sets the radius
	 * 
	 * @param r
	 */
	public void radius(double r) {
		radius = r;
	}
	
	/**
	 * Is this a site?
	 * 
	 * @return
	 */
	public boolean isTourSite() {
		return site.index > -1;
	}
	
	/**
	 * Returns the radius
	 * 
	 * @return
	 */
	public double radius() {
		return radius;
	}
	
	@Override
	protected void drawComponent(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		
		Color c = getSiteColor(site);
		gl.glColor3f(c.getRed()/255f, c.getGreen()/255f, c.getBlue());
		
		gl.glTranslatef(site.x, site.y, site.z);
		new GLU().gluSphere(theQuad, radius, slices, stacks);
		gl.glTranslatef(-site.x, -site.y, -site.z);
		
		gl.glNormal3f(0,0,1); // reset the normal position that gluSphere() messed up 
	}

	/**
	 * Returns the site color
	 * 
	 * @return
	 */
	private Color getSiteColor(Site s) {
		if(s.index > -1) return siteColor; 
		else if(site.isOnCurve) return onColor;
		else return offColor;
	}

	
	
}
